﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.IO;

namespace AresCustomYacht
{
    public class PageManager : System.Web.UI.Page
    {
        protected readonly BL _bl = new BL();
        protected readonly DAL _dal = new DAL();
        protected int _mode = 0;
        protected int PKId;
        protected string sedString;
        protected int BoatId;
        protected string BoatIdString;
        protected void InitializePageManager()
        {
            //todo: Check SId in the Query string. if it exists then make sure that the expiration date is still within. if not redirect to login

            var sed = Utility.GetQueryStringItem(this, "sed");
            BoatIdString = Utility.GetQueryStringItem(this, "BoatId");
            if (!string.IsNullOrEmpty(BoatIdString)) BoatId = int.Parse(BoatIdString);
            if (!string.IsNullOrEmpty(sed))
            {
                sedString = "sed=" + Utility.Encrypt(sed);
                //Try to parse the date
                DateTime sessionExpirationDate;
                if (DateTime.TryParse(sed,out sessionExpirationDate))
                {
                    //session expiration date is valid make sure it is still less than current time
                    if (DateTime.Now < sessionExpirationDate) //session expired
                    {
                        //Redirect to login
                        //Response.Redirect("http://aresmarine.com/haj/public/p-login.aspx");
                    }
                    else 
                    {
                        //Do nothing
                    }
                }
                else //Technical error
                {
                    //Session Id is invalid do not allow login
                }
            }
            _mode = Utility.GetPageMode(this); //Will default to zero if mode not found
            if (_mode != 0)
            {
                var PKIdstring = Utility.GetQueryStringItem(this, "PKId"); //Get primary key Id
                if (string.IsNullOrEmpty(PKIdstring))
                {
                    //_dal.LogException("PKId not found in Query string for page " + this.Title);
                }
                else
                {
                    //if (!int.TryParse(PKIdstring, out PKId))
                    //    _dal.LogException("Can't parse PKId value is " + PKIdstring + "   in page " + this.Title);
                }
            }



        }

        protected void RedirectAfterUpdate()
        {
            var physicalPath = Request.PhysicalPath;
            var redirectFileName = Path.GetFileNameWithoutExtension(physicalPath) + "s.aspx"; //Redirect to the Grid Page
            Response.Redirect(redirectFileName +  "?k=" + Utility.Encrypt("Message=Record Updated (" + PKId + ")"));
        }

        protected void RedirectAfterDelete()
        {
            var physicalPath = Request.PhysicalPath;
            var redirectFileName = Path.GetFileNameWithoutExtension(physicalPath) + "s.aspx"; //Redirect to the Grid Page
            Response.Redirect(redirectFileName + "?k=" + Utility.Encrypt("Message=Record Deleted (" + PKId + ")"));
        }

        protected void RedirectAfterInsert()
        {
            var physicalPath = Request.PhysicalPath;
            var redirectFileName = Path.GetFileNameWithoutExtension(physicalPath) + "s.aspx"; //Redirect to the Grid Page
            Response.Redirect(redirectFileName + "?k=" + Utility.Encrypt("Message=Record Inserted (" + PKId + ")"));
        }

        protected void RedirectAfterCancel()
        {
            var physicalPath = Request.PhysicalPath;
            var redirectFileName = Path.GetFileNameWithoutExtension(physicalPath) + "s.aspx"; //Redirect to the Grid Page
            Response.Redirect(redirectFileName + "?k=" + Utility.Encrypt("Message=Cancelled"));
        }

        protected string GenerateOnClientClick(string pageName, string clearQueryString)
        {
            return "window.open('" + pageName + "?k=" + Utility.Encrypt(clearQueryString) + "')";
        }

    }
}